<%@ Page Language="C#" Theme="Defaultsys" AutoEventWireup="true" CodeFile="Template.aspx.cs"
    Inherits="LetterTemplate" Title="Rapport Plan System - Template" MasterPageFile="~/MasterPage.master"
    EnableEventValidation="false" ValidateRequest="false" %>

<%@ MasterType VirtualPath="~/MasterPage.master" %>
<asp:Content runat="server" ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1">
    <!-- tinyMCE -->

    <script language="javascript" type="text/javascript" src="jscripts/tiny_mce/tiny_mce.js"></script>

    <script language="javascript" type="text/javascript">


	tinyMCE.init({
		theme : "advanced",
		elements : "ctl00_ContentPlaceHolder1_txtLetterTemplate",
		content_css : "example_advanced.css",
		extended_valid_elements : "a[href|target|name]",
		plugins : "table,style",
		theme_advanced_buttons3_add_before : "tablecontrols,separator",
		theme_advanced_buttons1_add : "fontselect,fontsizeselect",		
		relative_urls : false,
	    	convert_urls : false,
		theme_advanced_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1", 

		mode : "exact",   
		force_p_newlines : false,
		force_br_newlines : true,
		convert_newlines_to_brs : false,
		remove_linebreaks : true,   
   
    		debug : false   

		
	});
	
	

	// Custom event handler
	function myCustomExecCommandHandler(editor_id, elm, command, user_interface, value) {
		var linkElm, imageElm, inst;

		switch (command) {
			case "mceLink":
				inst = tinyMCE.getInstanceById(editor_id);
				linkElm = tinyMCE.getParentElement(inst.selection.getFocusElement(), "a");

				if (linkElm)
					alert("Link dialog has been overriden. Found link href: " + tinyMCE.getAttrib(linkElm, "href"));
				else
					alert("Link dialog has been overriden.");

				return true;

			case "mceImage":
				inst = tinyMCE.getInstanceById(editor_id);
				imageElm = tinyMCE.getParentElement(inst.selection.getFocusElement(), "img");

				if (imageElm)
					alert("Image dialog has been overriden. Found image src: " + tinyMCE.getAttrib(imageElm, "src"));
				else
					alert("Image dialog has been overriden.");

				return true;
		}

		return false; // Pass to next handler in chain
	}



    </script>

    <!-- /tinyMCE -->

    <script type="text/javascript" language="javascript">
        var aTags = new Array();
        var aColumns = new Array();
        
        function Balance()
        {
            var auxTags = document.getElementById("ctl00_ContentPlaceHolder1_txtTags").value.split(";");
            if (auxTags.length != aTags.length + 1)
            {
                var auxColumns = document.getElementById("ctl00_ContentPlaceHolder1_txtColumns").value.split(";");
                for (i=0; i<auxTags.length-1; i++)
                {
                    aTags[i] = auxTags[i];
                    aColumns[i] = auxColumns[i];
                }                
            }
        }
        
        function CreateNewTag(obj)
        {
            Balance();
            var newTag = "["+document.getElementById("ctl00_ContentPlaceHolder1_txtNewTag").value+"] "+
                document.getElementById("ctl00_ContentPlaceHolder1_cmbColumn").value;
            var o = document.createElement("option");
            o.text = newTag;
            o.value = newTag;
            try 
            {
                document.getElementById("ctl00_ContentPlaceHolder1_lstTags").add(o,null);
            }
            catch(ex) {
                document.getElementById("ctl00_ContentPlaceHolder1_lstTags").add(o);
            }
          
            aTags[aTags.length] = "["+document.getElementById("ctl00_ContentPlaceHolder1_txtNewTag").value+"]";
            aColumns[aColumns.length] = document.getElementById("ctl00_ContentPlaceHolder1_cmbColumn").value;
            RefreshTags();           
            
            return false;
        }   
        
        function RefreshTags()
        {
            document.getElementById("ctl00_ContentPlaceHolder1_txtTags").value = "";
            document.getElementById("ctl00_ContentPlaceHolder1_txtColumns").value = "";
            for (i=0; i<aTags.length; i++)
            {
                document.getElementById("ctl00_ContentPlaceHolder1_txtTags").value += aTags[i]+";";
                document.getElementById("ctl00_ContentPlaceHolder1_txtColumns").value += aColumns[i]+";";
            }
        }    
        
        function RemoveTag(obj)
        {         
            Balance();     
            var lst = document.getElementById("ctl00_ContentPlaceHolder1_lstTags");
            var auxTags = new Array();
            var auxColumns = new Array();
            if (lst.selectedIndex != -1)
            {
                for (i=0; i<lst.selectedIndex; i++)
                {
                    auxTags[auxTags.length] = aTags[i];
                    auxColumns[auxColumns.length] = aColumns[i];
                }
                
                for (i=lst.selectedIndex+1; i<aTags.length; i++)
                {
                    auxTags[auxTags.length] = aTags[i];
                    auxColumns[auxColumns.length] = aColumns[i];
                }
                
                aTags = auxTags;
                aColumns = auxColumns;
                
                RefreshTags();
                lst.remove(lst.selectedIndex);
            }
            
            return false;
        }   
        
        function AddTag(obj)
        {              
            var lst = document.getElementById("ctl00_ContentPlaceHolder1_lstTags");
            if (lst.selectedIndex != -1)
            {                   
		        tinyMCE.execCommand('mceInsertContent', false, lst.options[lst.selectedIndex].value);
            }
            
            return false;
        }        
    </script>

    <table border="0" width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td class="contTable">
                <table style="width: 100%">
                    <tr>
                        <td>
                            Name:
                        </td>
                        <td>
                            <asp:TextBox ID="txtName" runat="server" Width="550px"></asp:TextBox>
                            <asp:Label ID="lblSteluta" runat="server" ForeColor="Red" Text="*" Visible="False"></asp:Label></td>
                    </tr>
                    <tr>
                        <td valign="top">
                            Body:</td>
                        <td>
                            <asp:TextBox ID="txtLetterTemplate" runat="server" Height="300px" TextMode="MultiLine"
                                Width="455px"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td valign="top">
                            Logo:</td>
                        <td valign="top">
                            <asp:FileUpload ID="FileUpload1" runat="server" Width="640px" /><br />
                            <asp:Image ID="imgLogo" runat="server" />
                            <asp:CheckBox ID="ckRemoveLogo" runat="server" Text="Remove logo" /></td>
                    </tr>
                    <tr>
                        <td valign="top">
                            Tags:
                        </td>
                        <td>
                            <asp:ListBox ID="lstTags" runat="server" Width="565px" Height="200px"></asp:ListBox>
                            <asp:Button ID="btnAddTag" runat="server" Text="Add tag to template" />
                        </td>
                    </tr>
                    <tr>
                        <td align="left" colspan="2" style="height: 25px">
                            <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
                            <asp:Button ID="btnCancel" runat="server" OnClick="Cancel" Text="Cancel" />
                            <br />
                            <asp:Label ID="lblSavedTemplate" runat="server" ForeColor="#FF8000" Text="The template was saved successfully"
                                Visible="False"></asp:Label></td>
                    </tr>
                </table>
                <asp:Label ID="lblTemplateExist" runat="server" ForeColor="Red" Text="(*)Template name already exist"
                    Visible="false" Width="203px"></asp:Label>
            </td>
        </tr>
    </table>
</asp:Content>
